package cn.glc.daily.life.web.service.captcha.impl;

import cn.glc.daily.life.model.dto.SendCodeDto;
import cn.glc.daily.life.web.service.captcha.abs.AbstractSendCode;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.regex.Pattern;

/**
 * 短信发送实现
 *
 * @author simon
 * @date 2020/6/1 16:27
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@Component("sms")
public class SmsCodeSend extends AbstractSendCode {

    private final String regexp = "^[1]\\d{10}$";
    private final Pattern pattern = Pattern.compile(regexp);

    @Override
    public boolean vail(SendCodeDto sendCode) {
        Assert.assertNotNull("手机号不能为空", sendCode);
        Assert.assertTrue("请输入正确的手机号", pattern.matcher(sendCode.getNumber()).matches());
        return true;
    }

    @Override
    public boolean send(SendCodeDto sendCode) {
        Optional.ofNullable(vail(sendCode)).orElseThrow(() -> new IllegalArgumentException());
        log.info("手机验证码发送成功");
        return true;
    }

    @Override
    public boolean destroy(SendCodeDto sendCode) {
        return false;
    }
}
